<?php 
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
if(isset($_REQUEST['lchk'])){
	if($_REQUEST['lchk']!=''){
		$str=$_REQUEST['lchk'];
		$mang=split(",", $str);
		$sl=count($mang);
		if($sl>0){
			$connection = mysql_connect("1.234.53.52", "cam", "5678" ) ;
			mysql_select_db("testbk130108", $connection);
			mysql_query("set names 'utf8'");
			$sql="insert into approvedoc (IDApproveDoc,Tile,Content,IDEmployee,IDGroupApprove,SumMoney,Timecreate,IDStatus,IDTypeDoc,TimeApprove,`comment`,Description,purpose,Location,TripDate,TotalTime,EmployeePerforment,Content2,isView,IDLabel) VALUES ";
			
			$path="backupsql/";
			$tenfile="backup_".$_SESSION['user']."_".date('Y-m-d-H-i-s').".sql";
			$path .=$tenfile;
			$substr='';
			$ff=fopen($path, "w+");
			if($ff != false && $ff!=0){
				include 'obj_app.php';
				$ob= new appobj();
				$lgroup='';
				$lid='';
				$lapp='insert into listapprove (ID,IDGroupApprove,IDEmployee,IDtypeApprove,`Order`,IDStatus,DateApprove,Description,Signature,isView,IDLabel) values ';
				$lrefer='insert into list_reference (id,id_employee,id_manager,id_group,id_status,IDLabel) values ';
				$ladvance="insert into ap_advance (id,items,content,money,notes,document_id) values ";
				$lbuy='insert into buying_document (id,document_id,`name`,company,amount,unit,price,vat,total) values ';
				for($i=0;$i<$sl;$i++){
					$ob=unserialize (serialize ($_SESSION['bklist'][$mang[$i]]));
					if($substr==''){
						$substr = "('".$ob->idapprovedoc."','".chop($ob->title)."','".chop($ob->content)."','".$ob->idemployee."','".$ob->idgroupapprove."',".$ob->summoney.",'".$ob->timecreat."',".$ob->idstatus.",".$ob->idtypedoc.",'".$ob->timeapprove."','".$ob->comment."','".$ob->description."','".$ob->purpose."','".$ob->location."','".$ob->tripdate."','".$ob->totaltime."','".$ob->employeeperforment."','".$ob->content2."',".$ob->isview.",".$ob->idlabel.")";
					}else{
						$substr .= ",('".$ob->idapprovedoc."','".chop($ob->title)."','".chop($ob->content)."','".$ob->idemployee."','".$ob->idgroupapprove."',".$ob->summoney.",'".$ob->timecreat."',".$ob->idstatus.",".$ob->idtypedoc.",'".$ob->timeapprove."','".$ob->comment."','".$ob->description."','".$ob->purpose."','".$ob->location."','".$ob->tripdate."','".$ob->totaltime."','".$ob->employeeperforment."','".$ob->content2."',".$ob->isview.",".$ob->idlabel.")";
					}
					
					if($lid==''){
						$lid="'".$ob->idapprovedoc."'";
					}else{
						$lid .=",'".$ob->idapprovedoc."'";
					}
					
					if($lgroup==''){
						$lgroup = "'".$ob->idgroupapprove."'";
					}else{
						$lgroup .= ",'".$ob->idgroupapprove."'";
					}
				}
				
				if($substr!=''){
					$sql .=$substr." ; ";
					$kq=fwrite($ff,$sql);
				}
				$t1='';
				$t2='';
				if($lid!=''){
					$s1="select * from ap_advance where ap_advance.document_id in (".$lid.")";
					$m1=mysql_query($s1);
					if(mysql_num_rows($m1)>0){
						while($r1=mysql_fetch_array($m1)){
							if($t1==''){
								$t1 ="(".$r1['id'].",'".$r1['items']."','".$r1['content']."',".$r1['money'].",'".$r1['notes']."','".$r1['document_id']."')";
							}else{
								$t1 .=",(".$r1['id'].",'".$r1['items']."','".$r1['content']."',".$r1['money'].",'".$r1['notes']."','".$r1['document_id']."')";
							}
						}
					}
					
					$s2="select * from buying_document where buying_document.document_id in (".$lid.")";
					$m2=mysql_query($s2);
					if(mysql_num_rows($m2)>0){
						while($r2=mysql_fetch_array($m2)){
							if($t2==''){
								$t2 ="('".$r2['id']."','".$r2['document_id']."','".$r2['name']."','".$r2['company']."',".$r2['amount'].",'".$r2['unit']."',".$r2['price'].",".$r2['vat'].",".$r2['total'].")";
							}else{
								$t2 .=",('".$r2['id']."','".$r2['document_id']."','".$r2['name']."','".$r2['company']."',".$r2['amount'].",'".$r2['unit']."',".$r2['price'].",".$r2['vat'].",".$r2['total'].")";
							}
						}
					}
					
				}
				
				if($t1!=''){
					$ladvance .=$t1." ; ";
					$kq1=fwrite($ff,$ladvance);
				}
				
				if($t2!=''){
					$lbuy .=$t2." ; ";
					$kq2=fwrite($ff,$lbuy);
				}
				
				$t3='';
				$t4='';
				if($lgroup!=''){
					$s3="select * from listapprove where listapprove.IDGroupApprove in (".$lgroup.")";
					$m3=mysql_query($s3);
					if(mysql_num_rows($m3)>0){
						while($r3=mysql_fetch_array($m3)){
							if($t3==''){
								$t3 ="(".$r3['ID'].",'".$r3['IDGroupApprove']."','".$r3['IDEmployee']."',".$r3['IDtypeApprove'].",".$r3['Order'].",".$r3['IDStatus'].",'".$r3['DateApprove']."','".$r3['Description']."','".$r3['Signature']."',".$r3['isView'].",".$r3['IDLabel'].")";
							}else{
								$t3 .=",(".$r3['ID'].",'".$r3['IDGroupApprove']."','".$r3['IDEmployee']."',".$r3['IDtypeApprove'].",".$r3['Order'].",".$r3['IDStatus'].",'".$r3['DateApprove']."','".$r3['Description']."','".$r3['Signature']."',".$r3['isView'].",".$r3['IDLabel'].")";
							}
						}
					}
					
					$s4="select * from list_reference where list_reference.id_group in (".$lgroup.")";
					$m4=mysql_query($s4);
					if(mysql_num_rows($m4)>0){
						while($r4=mysql_fetch_array($m4)){
							if($t4==''){
								$t4 ="(".$r4['id'].",'".$r4['id_employee']."','".$r4['id_manager']."','".$r4['id_group']."',".$r4['id_status'].",".$r4['IDLabel'].")";
							}else{
								$t4 .=",(".$r4['id'].",'".$r4['id_employee']."','".$r4['id_manager']."','".$r4['id_group']."',".$r4['id_status'].",".$r4['IDLabel'].")";
							}
						}
					}
				}
				
				if($t3!=''){
					$lapp .=$t3." ; ";
					$kq3=fwrite($ff,$lapp);
				}
				
				if($t4!=''){
					$lrefer .=$t4." ; ";
					$kq4=fwrite($ff,$lrefer);
				}
				
				fclose($ff);
				if($kq!=FALSE ){
					if($lgroup!=''){
						$q1="delete from listapprove where listapprove.IDGroupApprove in (".$lgroup.")";
						$q2="delete from list_reference where list_reference.id_group in (".$lgroup.")";
						mysql_query($q1);
						mysql_query($q2);
					}
					if($lid!=''){
						$q3="delete from ap_advance where ap_advance.document_id in (".$lid.")";
						$q4="delete from buying_document where buying_document.document_id in (".$lid.")";
						$q="delete from approvedoc where approvedoc.IDApproveDoc in (".$lid.")";
						mysql_query($q3);
						mysql_query($q4);
						mysql_query($q);
					}
				
					$path="backupsql";
					$dir= opendir($path);
					while(($file = readdir($dir))!==false){
						$ten =$path."/".$file;
						if(is_file($ten)==TRUE){
							$mkt=split("_", $file);
							if(isset($mkt[1]) && $mkt[1]==$_SESSION['user']){
								?>
								<a onclick="clickdownload('<?=$file?>');" style="cursor:pointer;color:blue;">
									<b>
						    			<?php echo "Backup : ".$file." [ Download ]";?>
						    		</b>
						    	</a>
						    	<br>
								<?php 
							}
							
						}
					}
					closedir($dir);
				}
			}else{
				echo "lỗi mở file";
			}
			
			mysql_close($connection);
		}
	}
}

?>